<?php
/**
 * Created by PhpStorm.
 * User: PGF(592472116@qq.com)
 * Date: 2016/8/22
 * Time: 10:48
 */

namespace Api\Controller;
use Think\Controller;
use Com\Message;
header("content-type:text/html;charset=utf-8");
class NotifyController extends Controller
{
	//--支付宝异步通知地址
	function index(){
        file_put_contents('./alipay.txt',json_encode($_POST));
		include APP_PATH.'../Alipay1/Alipay.class.php';
		//$alipay = new \Alipay();
		//$res = $alipay->verifyNotify();
        if($_POST['trade_status']!='TRADE_SUCCESS'){
            exit;
        }
        $res['trade_no']=$_POST['out_trade_no'];
        $res['total_fee']=$_POST['total_amount'];
        $order_no=$res['trade_no'];
        $where['order_no'] = $order_no;
        $where['status'] = 0;
        $where['eff_time'] =array('gt',time());
        $recharge = M('recharge_order')->where($where)->find();
        $sql = M('recharge_order')->getLastSql();
        file_put_contents('./sql.txt',json_encode($sql));
        if($recharge){
            file_put_contents('./recharge.txt',json_encode($recharge));
            //$pay_status=M('recharge_order')->where(array('order_no' => $order_no))->getField('status');
            if ($res['total_fee'] != $recharge['amount']) {
                exit;
            }
            if ($recharge['status']==1) {
                echo 'SUCCESS';
                exit;
            }
            //增加用户 惠Go币
            M('member')->where('uid=' . $recharge['uid'])->setInc('amount', $recharge['amount']);
            //更新充值记录
            $map['status'] = 1;
            $map['update_time'] = time();
            M('recharge_order')->where(array('order_no' => $order_no))->save($map);

            //邀请奖励处理
            $member = M('member')->where('uid=' . $recharge['uid'])->find();
            if ($member['referrer']) {
                $refer = M('member')->where('uid=' . $member['referrer'])->find();
                if ($refer) {
                    $coin = $recharge['amount'] * 10;
                    M('member')->where('uid=' . $member['referrer'])->setInc('coin', $coin);
                    //邀请奖励记录
                    $refer_data['uid'] = $member['referrer'];
                    $refer_data['order_no'] = $order_no;
                    $refer_data['type'] = 5;
                    $refer_data['money'] = $coin;
                    $refer_data['month'] = date('Ym', time());
                    $refer_data['status'] = 1;
                    $refer_data['b_refer'] = $recharge['uid'];
                    $refer_data['createtime'] = time();
                    $re = M('user_trade')->add($refer_data);
                    $result['order_no'] = $order_no;
                }
            }

            echo 'SUCCESS';

        }
	}

}